package LC04;

import java.util.List;

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

public class LC0328 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode curr = head;
        curr.next = new ListNode(2);
        curr = curr.next;
        curr.next = new ListNode(3);
        curr = curr.next;
        curr.next = new ListNode(4);
        curr = curr.next;
        curr.next = new ListNode(5);
        System.out.println(oddEvenList(head));
    }

    public static ListNode oddEvenList(ListNode head) {
        if (head ==null || head.next == null) {
            return head;
        }
        ListNode curr = head;
        ListNode a = curr;
        curr = curr.next;
        ListNode b = curr;
        curr = curr.next;

        int i = 0;
        while (curr != null) {
            ListNode next = curr.next;
            if (i % 2 == 0) {
                ListNode cc = new ListNode(curr.val);
                ListNode anext = a.next;
                a.next = cc;
                cc.next = anext;
                b.next = next;
                a = cc;
                curr = next;
            } else {
                b = curr;
                curr = next;
            }
            i++;
        }
        return head;
    }
}
